﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using final_darskade.Models;

namespace final_darskade.Controllers
{
    public class AddToClassController : Controller
    {
        //
        // GET: /AddToClass/

        public ActionResult Index(int id)
        {
            darskadeEntities db = new darskadeEntities();
            //if((from p in db.klass_members where p.klass_id==id select p)==null)
            var klass = (from p in db.klass_members where p.klass_id == id select p).ToList();
            if (klass == null)
            {
                RedirectToAction("klassShow", "klassManagement");
            }


            return View("Index", klass);

        }

        //
        // GET: /AddToClass/Details/5

        public ActionResult Details(int? id)
        {
            //DetailOfUser student = new DetailOfUser();
            darskadeEntities db = new darskadeEntities();
            var student = (from p in db.users where p.user_id==id select p ).ToList();
            return View("Details",student);
        }

        //
        // GET: /AddToClass/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /AddToClass/Create

        [HttpPost]
        public ActionResult Create(int member_id,int id)
        {
            darskadeEntities db = new darskadeEntities();
            klass_members memToCreat = (from p in db.klass_members where p.member_id == member_id select p).FirstOrDefault();
            int defId = memToCreat.member_id;
            klass_members klassmem = new klass_members();

            if (defId!= null)
            {
                ModelState.AddModelError(" member_id","this member already exist");
            }
            else
            {
                klassmem.klass_id = id;
                klassmem.member_id = member_id;
                db.AddToklass_members(klassmem);
                db.SaveChanges();
            }
            
            return RedirectToAction("klassShow","klassManagement");
        }
        
        //
        // POST: /AddToClass/Delete/5

       
        public ActionResult Delete(int id)
        {
            darskadeEntities db= new darskadeEntities();
           // klass_members memToDelete = (klass_members)(from p in db.klass_members where p.member_id == id select p).Single();
            var query = (from p in db.klass_members where p.member_id == id select p);
            foreach (var row in query)
            {
                klass_members memToDelete = (klass_members)row;
                db.klass_members.DeleteObject(row);
            }
            db.SaveChanges();
            //db.klass_members.DeleteObject(memToDelete);
            //db.SaveChanges();
            
            return RedirectToAction("klassShow","klassManagement");
        }
        public ActionResult Search()
        {

            return View("Search");
        }
        [HttpPost]
        public ActionResult Search(int? user_id,string username)
        {
            darskadeEntities db = new darskadeEntities();
            user userToFind;
            if (user_id == null)
            {
                userToFind = (user)(from p in db.users where p.username.Equals(username) select p).FirstOrDefault();
            }
            else
            {
                userToFind = (user)(from p in db.users where p.user_id == user_id select p).FirstOrDefault();

            }
            userToFind = (user)(from p in db.users where p.user_id == user_id select p).FirstOrDefault();
            if (userToFind == null)
            {
                return RedirectToAction("Search");
            }
            return View("Search1",userToFind);
        }

        public ActionResult Privilege(int id)
        {
            return View("Privilege");
        }
    }
}
